<?php
class User extends Controller {

    private $aPost;
    private $aUser;

    function User() {

        parent::Controller();
        $this->load->model('model_user');

        //verificare sesiune
        if (!$this->usersession->userLoggedIn()) {
            $aResponse = array("error" => true, "description" => "Sesiunea a expirat", "type" => "nosession");
            die(json_encode($aResponse));
        }
        copyPost($this->aPost);
        $this->aUser = $this->usersession->currentUser();
    }

    /**
     * Functie care aduce datele in grid
     */
    function getData() {
        echo json_encode($this->model_user->getData($_POST));
    }

    /**
     * Functie de editare / inserare element
     * @return string json
     */
    function operation() {

        $bResponse = $this->model_user->operation($this->aPost);

        if ($bResponse === TRUE) {
            $aResponse = array("error" => false, "success" => true, "description" => "Datele au fost salvate");
        }
        else {
            $aResponse = array("error" => true, "description" => $bResponse, "type" => "Validare");
        }
        echo  json_encode($aResponse);
    }

    /**
     * Functie care aduce datele despre unei inregistrari
     * @return string json
     */
    function getRecord() {
        $aResult = $this->model_user->getRecord($this->aPost);
        echo json_encode(array("error" => false, "data" => $aResult));
    }

    /**
     * Functie care seteaza theme-ul unui user
     */
    function setStyle() {
        $this->model_user->setStyle($this->aPost["style"]);
    }

    /**
     * Functie care salveaza semnatura
     */
    function saveSign() {
        $aUser = $this->usersession->currentUser();
        $this->db->set("sign", $this->aPost["sign"]);
        $this->db->where("user_id", $aUser["user_id"]);
        $this->db->update("user");
        $aResponse = array("error" => false, "description" => "Semnatura a fost schimbata");
        echo json_encode($aResponse);
    }

    /**
     * Functie aduce semnatura
     */
    function getSign() {
        $aUser = $this->usersession->currentUser();
        $this->db->set("sign", $this->aPost["sign"]);
        $this->db->where("user_id", $aUser["user_id"]);
        $sSign = $this->db->get("user")->row();
        $aResponse = array("error" => false, "data" => $sSign);
        echo json_encode($aResponse);
    }


    /** Trimitere semnatura pe email
     * @return
     */
    function sendSign() {

        $this->load->plugin('phpmailer');
        $path_full = $this->config->item('upload_logo');

        $this->db->where("user_id", $this->aUser["user_id"]);
        $aUser = $this->db->get("user")->row_array();

        $this->db->where("code", "logo");
        $sLogo = $this->db->get("settings")->row()->value;

        $mail = new phpmailer(TRUE);

        $mail->IsHTML(TRUE);
        $mail->IsSMTP();
        $mail->SMTPAuth = TRUE;

        try {

            $mail->CharSet = "utf-8";
			
			$mail->Host = "mail.morethanpub.ro";
			$mail->Password = "officeoffice";
			$mail->Username = "office@morethanpub.ro";
			
            /*$mail->Host = "comunicatedirectmarketing.ro";
			$mail->Password = "marketing2006";
			$mail->Username = "office@comunicatedirectmarketing.ro";*/
			
			/*$mail->Password = "bookbook2013";
			$mail->Username = "bookland@comunicatedirectmarketing.ro";*/
            $mail->Port = 26;


            $mail->AddReplyTo($this->aUser["email"], $this->aUser["name"]);
            $mail->SetFrom("office@morethanpub.ro", "MTP CRM");
            $mail->AddAddress($this->aUser["email"]);

            $filename = "././resources/logo/" . $sLogo;
            $body = $aUser["sign"];


            if (file_exists($filename)) {

                $pattern = '/_LOGO/';
                preg_match_all($pattern, $body, $matches);

                if (count($matches[0]) > 0) {
                    //scot calea relativa
                    $body = str_replace($path_full, "", $body);
                    //$sReplace="cid:".$attach["name"];
                    $aReplace = explode(".", $sLogo);
                    array_pop($aReplace);
                    $base = implode(".", $aReplace);
                    $sReplace2 = "cid:" . $base;

                    $body = str_replace("_LOGO", "<img src='$sReplace2'  />", $body);
                    $mail->AddEmbeddedImage($filename, $base, $sLogo);
                }
            }

            $mail->Subject = "MTP - Semnatura Test";
            $mail->Body = "<body>" . $body . "</body>";

            if (!$mail->Send()) {
                $aResponse = array("error" => TRUE, "description" => "Emailul NU a fost trimis: " . $mail->ErrorInfo);
            }
            else {
                $aResponse = array("error" => FALSE, "description" => "Emailul a fost trimis");
            }
        }
        catch (phpmailerException $e) {
            $aResponse = array("error" => TRUE, "description" => "Emailul NU a fost trimis: " . $e->errorMessage(), "type" => "email");
        }
        catch (Exception $e) {
            $aResponse = array("error" => TRUE, "description" => "Emailul NU a fost trimis: " . $e->getMessage(), "type" => "email");
        }
        echo json_encode($aResponse);
    }

    /**
     * Functie care schimba parola unui user
     * @return string json
     */
    function addPass() {
        $bResult = $this->model_user->addPass($this->aPost);
        $aResponse = array("error" => false, "description" => "Parola a fost schimbata");
        echo json_encode($aResponse);
    }

}
